<template>
  <div class="login-box">
    <el-form class="form-box" ref="form" :rules="rules" :model="form" label-width="80px">
      <h2 class="login-title">疫情查询系统</h2>
      <el-form-item label="用户" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">登陆</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login, getUserInfo } from "@/api/login";
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
         // console.log("submit!");
          login(this.form.username, this.form.password).then(response => {
            const resp = response.data;
            console.log(resp)
            const flag=resp.success;
            if (resp.success) {
              console.log(888888)
              this.$router.push("/member");
              //验证成功  通过token去获取用户信息
              // getUserInfo(resp.data.token).then(response => {
              //   console.log(response.data);
              //   const respUser = response.data;
              //   if (respUser.flag) {
              //     //1、要保存token  2、保存用户信息
              //     localStorage.setItem(
              //       "mxg-msm-user",
              //       JSON.stringify(respUser.data)
              //     );
              //     localStorage.setItem(
              //       "mxg-msm-token",
              //       JSON.stringify(resp.data.token)
              //     );
              //     //前往首页
              //     this.$router.push("/");
              //   } else {
              //     //获取用户信息的错误提示
              //     this.$message({
              //       message: respUser.message,
              //       type: "warning"
              //     });
              //   }
              // });
            } else {
              //登陆的错误提示
              this.$message({
                message: resp.message,
                type: "warning"
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
.login-box {
  width: 100%;
  height: 100%;
  background: url(../../assets/login.jpg);
  position: absolute;
  overflow: hidden;
}
.form-box {
  width: 400px;
  height: 260px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  padding: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login-title {
  color: #303133;
  text-align: center;
}
</style>
